package UML_ER.diagram.navigator;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import UML_ER.Aggregation;
import UML_ER.BinaryRelationship;
import UML_ER.Composition;
import UML_ER.Generalization;
import UML_ER.Relationship;
import UML_ER.diagram.edit.parts.AggregationEditPart;
import UML_ER.diagram.edit.parts.AggregationNameEditPart;
import UML_ER.diagram.edit.parts.Attribute2EditPart;
import UML_ER.diagram.edit.parts.AttributeEditPart;

import UML_ER.diagram.edit.parts.BinaryRelationship2EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipEditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipNameEditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipTargetEditPart;
import UML_ER.diagram.edit.parts.CompositionEditPart;
import UML_ER.diagram.edit.parts.CompositionNameEditPart;
import UML_ER.diagram.edit.parts.ERmodelEditPart;
import UML_ER.diagram.edit.parts.EntityEditPart;
import UML_ER.diagram.edit.parts.EntityNameEditPart;
import UML_ER.diagram.edit.parts.GeneralizationEditPart;
import UML_ER.diagram.edit.parts.OntologyReference2EditPart;
import UML_ER.diagram.edit.parts.OntologyReferenceEditPart;
import UML_ER.diagram.edit.parts.OntologyReferenceRelationshipEditPart;
import UML_ER.diagram.edit.parts.RelationshipEditPart;
import UML_ER.diagram.edit.parts.RelationshipNameEditPart;
import UML_ER.diagram.edit.parts.RoleEditPart;
import UML_ER.diagram.edit.parts.RoleNameEditPart;
import UML_ER.diagram.part.UML_ERDiagramEditorPlugin;
import UML_ER.diagram.part.UML_ERVisualIDRegistry;
import UML_ER.diagram.providers.UML_ERElementTypes;
import UML_ER.diagram.providers.UML_ERParserProvider;

/**
 * @generated
 */
public class UML_ERNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		UML_ERDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		UML_ERDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof UML_ERNavigatorItem
				&& !isOwnView(((UML_ERNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof UML_ERNavigatorGroup) {
			UML_ERNavigatorGroup group = (UML_ERNavigatorGroup) element;
			return UML_ERDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof UML_ERNavigatorItem) {
			UML_ERNavigatorItem navigatorItem = (UML_ERNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (UML_ERVisualIDRegistry.getVisualID(view)) {
		case ERmodelEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?UML_ER?ERmodel", UML_ERElementTypes.ERmodel_1000); //$NON-NLS-1$
		case GeneralizationEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?UML_ER?Generalization", UML_ERElementTypes.Generalization_4011); //$NON-NLS-1$
		case AggregationEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?UML_ER?Aggregation", UML_ERElementTypes.Aggregation_4015); //$NON-NLS-1$
		case EntityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?UML_ER?Entity", UML_ERElementTypes.Entity_2003); //$NON-NLS-1$
		case RoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?UML_ER?Role", UML_ERElementTypes.Role_4024); //$NON-NLS-1$
		case BinaryRelationship2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?UML_ER?BinaryRelationship", UML_ERElementTypes.BinaryRelationship_4014); //$NON-NLS-1$
		case RelationshipEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?UML_ER?Relationship", UML_ERElementTypes.Relationship_2009); //$NON-NLS-1$
		case BinaryRelationshipTargetEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?UML_ER?BinaryRelationship?target", UML_ERElementTypes.BinaryRelationshipTarget_4022); //$NON-NLS-1$
		case BinaryRelationshipEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?UML_ER?BinaryRelationship", UML_ERElementTypes.BinaryRelationship_2008); //$NON-NLS-1$
		case CompositionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?UML_ER?Composition", UML_ERElementTypes.Composition_4025); //$NON-NLS-1$
		case AttributeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?UML_ER?Attribute", UML_ERElementTypes.Attribute_3001); //$NON-NLS-1$
		case Attribute2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?UML_ER?Attribute", UML_ERElementTypes.Attribute_3003); //$NON-NLS-1$
		case OntologyReferenceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?UML_ER?OntologyReference", UML_ERElementTypes.OntologyReference_3002); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = UML_ERDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& UML_ERElementTypes.isKnownElementType(elementType)) {
			image = UML_ERElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof UML_ERNavigatorGroup) {
			UML_ERNavigatorGroup group = (UML_ERNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof UML_ERNavigatorItem) {
			UML_ERNavigatorItem navigatorItem = (UML_ERNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (UML_ERVisualIDRegistry.getVisualID(view)) {
		case ERmodelEditPart.VISUAL_ID:
			return getERmodel_1000Text(view);
		case GeneralizationEditPart.VISUAL_ID:
			return getGeneralization_4011Text(view);
		case AggregationEditPart.VISUAL_ID:
			return getAggregation_4015Text(view);
		case EntityEditPart.VISUAL_ID:
			return getEntity_2003Text(view);
		case RoleEditPart.VISUAL_ID:
			return getRole_4024Text(view);
		case BinaryRelationship2EditPart.VISUAL_ID:
			return getBinaryRelationship_4014Text(view);
		case RelationshipEditPart.VISUAL_ID:
			return getRelationship_2009Text(view);
		case BinaryRelationshipTargetEditPart.VISUAL_ID:
			return getBinaryRelationshipTarget_4022Text(view);
		case BinaryRelationshipEditPart.VISUAL_ID:
			return getBinaryRelationship_2008Text(view);
		case CompositionEditPart.VISUAL_ID:
			return getComposition_4025Text(view);
		case AttributeEditPart.VISUAL_ID:
			return getAttribute_3001Text(view);
		case Attribute2EditPart.VISUAL_ID:
			return getAttribute_3003Text(view);
		case OntologyReferenceEditPart.VISUAL_ID:
			return getOntologyReference_3002Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getGeneralization_4011Text(View view) {
		Generalization domainModelElement = (Generalization) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4011); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getERmodel_1000Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getEntity_2003Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Entity_2003,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry.getType(EntityNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRole_4024Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Role_4024,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry.getType(RoleNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6008); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getBinaryRelationshipTarget_4022Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getOntologyReference_3002Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.OntologyReference_3002,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry
						.getType(OntologyReferenceEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 3002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAttribute_3001Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Attribute_3001,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry.getType(AttributeEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 3001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getBinaryRelationship_4014Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.BinaryRelationship_4014,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry
						.getType(BinaryRelationshipNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getComposition_4025Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Composition_4025,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry
						.getType(CompositionNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6015); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRelationship_2009Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Relationship_2009,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry
						.getType(RelationshipNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getBinaryRelationship_2008Text(View view) {
		BinaryRelationship domainModelElement = (BinaryRelationship) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2008); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAttribute_3003Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Attribute_3003,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry.getType(Attribute2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 3003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAggregation_4015Text(View view) {
		IParser parser = UML_ERParserProvider.getParser(
				UML_ERElementTypes.Aggregation_4015,
				view.getElement() != null ? view.getElement() : view,
				UML_ERVisualIDRegistry
						.getType(AggregationNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			UML_ERDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6012); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return ERmodelEditPart.MODEL_ID.equals(UML_ERVisualIDRegistry
				.getModelID(view));
	}

}
